package com.oa.action;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.oa.bean.work.Appliance;
import com.oa.bean.work.Atype;
import com.oa.bean.work.Dept;
import com.oa.bean.work.Emp;
import com.oa.service.WorkService;
import com.opensymphony.xwork2.ActionSupport;


public class WorkAction extends ActionSupport{

	private static final long serialVersionUID = 4166298476782886358L;
	
	private WorkService workService;
	private List<Dept> deptlist;
	private Dept dept;
	private List<Emp> emplist;
	private Emp emp;
	
	public void setWorkService(WorkService workService) {
		this.workService = workService;
	}
	
	public Dept getDept() {
		return dept;
	}
	
	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public List<Dept> getDeptlist() {
		return deptlist;
	}

	public void setDeptlist(List<Dept> deptlist) {
		this.deptlist = deptlist;
	}

	public List<Emp> getEmplist() {
		return emplist;
	}

	public void setEmplist(List<Emp> emplist) {
		this.emplist = emplist;
	}

	public Emp getEmp() {
		return emp;
	}

	public void setEmp(Emp emp) {
		this.emp = emp;
	}
	
	private Atype atype;
	private Appliance appliance;
	
	public Atype getAtype() {
		return atype;
	}
	public void setAtype(Atype atype) {
		this.atype = atype;
	}
	public Appliance getAppliance() {
		return appliance;
	}
	public void setAppliance(Appliance appliance) {
		this.appliance = appliance;
	}

	
	//办公用品类型添加
	public String atypeAdd(){
		Atype a = workService.atypeSave(atype);
		System.out.println(a.toString());
		return SUCCESS;
	}
	
	//办公用品类型修改
	public String atypeUpdate(){
		workService.atypeUpdate(atype);
		return SUCCESS;
	}
	
	//办公用品类型删除
	public String atypeDelete(){
		workService.atypeDelete(atype);
		return SUCCESS;
	}
	
	//办公用品添加
	public String applianceAdd(){
		appliance.setCount(3);
		appliance.setPrice(new BigDecimal(100));
		Atype at = new Atype();
		at.setTypeid(1);
		appliance.setAtype(at);
		
		Appliance a = workService.applianceSave(appliance);
		System.out.println(a.toString());
		return SUCCESS;
	}
	
	//办公用品修改
	public String applianceUpdate(){
		appliance.setCount(3);
		appliance.setPrice(new BigDecimal(100));
		Atype at = new Atype();
		at.setTypeid(1);
		appliance.setAtype(at);
		
		workService.applianceUpdate(appliance);
		return SUCCESS;
	}
	
	//办公用品删除
	public String applianceDelete(){
		workService.applianceDelete(appliance);
		return SUCCESS;
	}
	
	//办公用品分页查询
	public String applianceQueryPage(){
		List<Appliance> a = workService.applianceQueryPager(0, 2);
		for(Appliance app : a){
			System.out.println(app.toString());
		}
		return SUCCESS;
	}

	public String deptAdd() {
		Date d = Calendar.getInstance().getTime();
		Dept depts = new Dept();
		depts.setDname("市场部");
		depts.setStatus(1);
		depts.setCreatetime(d);
		System.out.println(depts);
		workService.deptSave(depts);
		return SUCCESS;
	}
	
	public String deptUpdate() {
		Date date = Calendar.getInstance().getTime();
		Dept depts = new Dept();
		depts.setDid(3);
		depts.setDname("人事部");
		depts.setStatus(1);
		depts.setCreatetime(date);
		System.out.println(depts+"/*/*/*/*/*/*/*/*/");
		Dept d = workService.deptUpdate(depts);
		if(d != null) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String deptDelete() {
		workService.deptDelete(dept.getDid());
		return SUCCESS;
	}

	public String deptQueryPage() {
		deptlist = workService.deptQueryByPage(1, 3);
		for(Dept d : deptlist) {
			System.out.println(d);
		}
		workService.getclose();
		if(deptlist != null) {
			return SUCCESS;
		}
		return ERROR;
	}

	public String empAdd() {
		System.out.println("----------------");
		Emp e = new Emp();
		e.setEname("王老师");
		e.setEsex("男");
		Dept dept = new Dept();
		dept.setDid(1);
		e.setDept(dept);
		String bir = "1991-10-23";
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		try {
			e.setEbirth(sim.parse(bir));	//将String类型转换成Date类型
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		e.setEcertid("360732199110231710");
		e.setEcity("江西赣州");
		e.setEanton("汉");
		e.setEdu("本科");
		e.setEaddr("江西省赣州市章贡区");
		e.setEtry(Calendar.getInstance().getTime());
		e.setEcreate(Calendar.getInstance().getTime());
		e.setEstatus(1);
		e.setEpwd("123456");
		System.out.println(e+"***********");
		workService.empSave(e);
		return SUCCESS;
	}
	
	public String empUpdate() {
		System.out.println("----------------");
		Emp e = new Emp();
		e.setEid(2);
		e.setEname("朱老师");
		e.setEsex("男");
		Dept dept = new Dept();
		dept.setDid(1);
		e.setDept(dept);
		String bir = "1995-10-23";
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		try {
			e.setEbirth(sim.parse(bir));	//将String类型转换成Date类型
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		e.setEcertid("360732199110231710");
		e.setEcity("江西赣州");
		e.setEanton("汉");
		e.setEdu("本科");
		e.setEaddr("江西省赣州市章贡区");
		e.setEtry(Calendar.getInstance().getTime());
		e.setEcreate(Calendar.getInstance().getTime());
		e.setEstatus(1);
		e.setEpwd("123456");
		System.out.println(e);
		Emp em = workService.empUpdate(e);
		if(em != null) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String empDelete() {
		System.out.println(emp.getEid());
		workService.empDelete(emp.getEid());
		return SUCCESS;
	}
	
	public String empQueryPage() {
		emplist = workService.empQueryPage(1, 3);
		for(Emp e : emplist) {
			System.out.println(e);
		}
		workService.getclose();
		if(emplist != null) {
			return SUCCESS;
		}
		return ERROR;
	}
}
